package com.ruoyi.am.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.am.domain.AmDriving;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.am.domain.AmApply;
import com.ruoyi.am.service.IAmApplyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 申请备班Controller
 * 
 * @author hkkk
 * @date 2020-12-19
 */
@Controller
@RequestMapping("/am/apply")
public class AmApplyController extends BaseController
{
    private String prefix = "am/apply";

    @Autowired
    private IAmApplyService amApplyService;

    @RequiresPermissions("am:apply:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    /**
     * 查询申请备班列表
     */
    @RequiresPermissions("am:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmApply amApply)
    {
        startPage();
        List<AmApply> list = amApplyService.selectAmApplyList(amApply);
        return getDataTable(list);
    }

    /**
     * 导出申请备班列表
     */
    @RequiresPermissions("am:apply:export")
    @Log(title = "申请备班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmApply amApply)
    {
        List<AmApply> list = amApplyService.selectAmApplyList(amApply);
        ExcelUtil<AmApply> util = new ExcelUtil<AmApply>(AmApply.class);
        return util.exportExcel(list, "apply");
    }

    /**
     * 新增申请备班
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存申请备班
     */
    @RequiresPermissions("am:apply:add")
    @Log(title = "申请备班", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmApply amApply)
    {
        return toAjax(amApplyService.insertAmApply(amApply));
    }

    /**
     * 修改申请备班
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AmApply amApply = amApplyService.selectAmApplyById(id);
        mmap.put("amApply", amApply);
        return prefix + "/edit";
    }

    /**
     * 修改保存申请备班
     */
    @RequiresPermissions("am:apply:edit")
    @Log(title = "申请备班", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmApply amApply)
    {
        return toAjax(amApplyService.updateAmApply(amApply));
    }

    /**
     * 删除申请备班
     */
    @RequiresPermissions("am:apply:remove")
    @Log(title = "申请备班", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amApplyService.deleteAmApplyByIds(ids));
    }

    @PostMapping("/postApply")
    @ResponseBody
    public AjaxResult postData(@RequestParam String data){
        JSONObject object=JSONObject.parseObject(data);
        AmApply amApply=JSONObject.toJavaObject(object, AmApply.class);
        return toAjax(amApplyService.insertAmApply(amApply));
    }

}
